#!/usr/bin/perl
# 2014/11/11 Remove barcode related code to apply HiC
# 2013/09/12 implement barcode check and output uniqueness report
# 2012/11/12 removing potentially PCR biassed data (if linker is different, allow duplicate)

use strict;
use warnings;
use IO::File;
use Getopt::Std;
use Carp qw(croak);
$| = 0;

if(@ARGV != 4 or $ARGV[0] eq '--help'){
	die "Usage : $0 -i [map file] -o [output filtered map file]\n";
}

my %opt;
getopts("i:o:", \%opt);
my $FILE_map = $opt{i};
my $FILE_out = $opt{o};

my $COUNTER_BIAS = 0;
my $COUNTER_UNIQUE = 0;

# for checking uniqueness
my %uniqueness;
my %foundreadNum;
my %totalReadInCategories;

my $before = '';
#my %foundBarcode;
my $NUMBER = 0;
my $fh_out = IO::File->new($FILE_out, 'w') or die "cannot write $FILE_out: $!";
my $fh_map = IO::File->new($FILE_map) or die "cannot open $FILE_map: $!";
while($_ = $fh_map->getline()){
#	s/\r?\n//;	# 改行を加えるためにわざとコメントアウト
	my ($id, $chr1, $loc1, $direction1, $mapQ1, $hinID1, $hinLoc1, $chr2, $loc2, $direction2, $mapQ2, $hinID2, $hinLoc2, $type, $num, $locations) = split /\t/;

	# both side should have data
	if($chr1 eq 'NA' or $chr2 eq 'NA'){
		next;
	}

	# id
	$NUMBER++;
	$id = $NUMBER;

#	# check barcode
#	my ($readNum, $barcode) = split /:/, $id;
#	if($barcode eq ''){
#		die "cannot find barcode for $id\n";
#	}

	my $current = join("\t", $chr1, $loc1, $chr2, $loc2);
	if($before ne $current){
		$fh_out->print("$_");
#		%foundBarcode = ();
		$COUNTER_UNIQUE++;

#		&checkUniqueness(\%foundreadNum);
#		%foundreadNum = ();
	}else{
#		if(exists $foundBarcode{$barcode}){
			$COUNTER_BIAS++;
#		}else{
#			$fh_out->print("$_");
#			$COUNTER_UNIQUE++;
#		}
	}
	$before = $current;
#	$foundBarcode{$barcode} = 1;
#	$foundreadNum{$readNum} = $barcode;
}

$fh_out->close();
$fh_map->close();

my $SUM = $COUNTER_UNIQUE + $COUNTER_BIAS;
print "PCR biasness remove\n";
printf "Unique: %d (%.3f)\n", $COUNTER_UNIQUE, $COUNTER_UNIQUE / $SUM * 100;
printf "Bias: %d (%.3f)\n\n", $COUNTER_BIAS, $COUNTER_BIAS / $SUM * 100;


#my $fh_report = IO::File->new($FILE_report, 'w') or die "cannot write $FILE_report: $!";
#$fh_report->print("Sequence depth\tUnique reads(without barcode)\tUnique reads(with barcode)\tTotal reads in the category\n");
#foreach my $key(sort {$a <=> $b} keys %totalReadInCategories){
#	my $noBarcode = exists $uniqueness{'noBarcode'}{$key} ? $uniqueness{'noBarcode'}{$key} : 0;
#	my $withBarcode = exists $uniqueness{'withBarcode'}{$key} ? $uniqueness{'withBarcode'}{$key} : 0;
#	$fh_report->print("$key\t$noBarcode\t$withBarcode\t$totalReadInCategories{$key}\n");
#}
#$fh_report->close();

#sub checkUniqueness{
#	my ($ref) = @_;
#	my %barcode;
#	my $flag = 0;
#	foreach my $readNum(sort {$a <=> $b} keys %$ref){
#		my $category = (int($readNum / 200000)+1)*200000;
#		$totalReadInCategories{$category}++;
#		if($flag == 0){
#			$uniqueness{'noBarcode'}{$category}++;
#			$flag = 1;
#		}
#		unless(exists $barcode{$ref->{$readNum}}){
#			$uniqueness{'withBarcode'}{$category}++;
#			$barcode{$ref->{$readNum}} = 1;
#		}
#	}
#}